package com.xuyiwei.mayi.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xuyiwei.mayi.adapter.MessageAdapter;
import com.xuyiwei.mayi.constants.Constants;
import com.xuyiwei.mayi.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * Created by xuyiwei on 2019/4/28.
 */
@Component
@Slf4j
public class CustomerDealMq {

    @Autowired
    private MailService mailService;
    private MessageAdapter messageAdapter;

    /**
      {
         "header": {
             "interfaceType": "接口类型"
         },
         "content": {}
     }
     * @param json
     */
    @JmsListener(destination = "${messages.queue}")
    public void distribute(String json) {

        JSONObject jsonObject = JSON.parseObject(json);
        JSONObject header = jsonObject.getJSONObject("header");
        String interfaceType = header.getString("interfaceType");
        if(!StringUtils.isEmpty(interfaceType)){
            if(Constants.MESSAGE_INTERFACE_TYPE_EMAIL.equals(interfaceType)){
                messageAdapter = mailService;
            }
            messageAdapter.sendMsg(jsonObject.getJSONObject("content"));
        }
    }
}
